package com.mkz.producer.controller;

import java.util.Date;

import org.apache.curator.framework.CuratorFramework;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.zookeeper.util.ExclusiveLock;

@RestController
@Scope(value="request")
public class TestController {
	@Autowired
    private KafkaTemplate<String, String> kafkaTemplate;
	@Autowired
	private CuratorFramework zkCurator;
	
	@RequestMapping("/testProducer")
	public ResponseResult<String> testProducer() {
		System.out.println("开始执行。。。。。。。。。。"+new Date());
		ExclusiveLock lock = new ExclusiveLock(zkCurator, "/mkz/lock/global/producer");
		Boolean lockResult = lock.lock();
		System.out.println(lockResult);
		if(lockResult) {
			kafkaTemplate.send("my_test", "Test Producer....");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			lock.release();
		}
		System.out.println("执行完毕。。。。。。。。。。"+new Date());
		return ResponseResult.ok("成功发送消息...");
	}
}
